var Canva = {};
var BinaryTree ={root:null};
BinaryTree.add = function(value)
{	
	 p = new Element(value);
	if(BinaryTree.root == null)
		{
			BinaryTree.root=p;
		}else
		{
			BinaryTree.root.insert(p);
		}	
    		
}
function Element(value) {  
   this.value = value;
   this.left=null;
   this.right=null;
   this.insert=function insert(p) 
   {
      if (p.value < this.value)
            {
                if (this.left != null)
                {
                    this.left.insert(p);
                }
                else
                {
                   this. left = p;
                }
            }
            else
            {
                if (this.right != null)
                {
                    this.right.insert(p);
                }
                else
                {
                    this.right = p;
                }
            }      
   }
}
function drawl(element,xpos,ypos) {  
 var canvas = document.getElementById("painter");  
 var ctx = canvas.getContext("2d");
 ctx.strokeRect(xpos,ypos,5,5);
 ctx.strokeText(element.value, xpos + 15, ypos + 8);
 ypos = 50+ypos;
 //alert(element.value); 
 if (element.right != null)
{
   drawL(element.right, xpos + ((canvas.width  - xpos) / 2), ypos);
};
if (element.left != null)
{  
   drawl(element.left, Math.abs(xpos + ((-canvas.width  + xpos) / 2)), ypos);
};               
 
} 
   
function  init()
{      
	BinaryTree.add(1);
	BinaryTree.add(4);
	BinaryTree.add(2);
	BinaryTree.add(6);
	BinaryTree.add(3);
	BinaryTree.add(5);
	
	var canvas = document.getElementById("painter"); 
	drawl(BinaryTree.root, canvas.width / 2, 15);	
}


